<?php

namespace Cours\PromotionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Cours\QuizzBundle\Entity\Promotion;

class PromotionController extends Controller {

    /**
     * @Route("/create")
     * @Template()
     */
    public function createAction()
    {
        $unePromo = new Promotion();

        $formBuilder = $this->createFormBuilder($unePromo)
                ->add('code', 'text')
                ->add('libelle', 'text');

        $form = $formBuilder->getForm();
        $request = $this->get('request');

        if ($request->getMethod() == 'POST')
        {
            $form->bind($request);

            if ($form->isValid())
            {
                $unePromo->setLibelle(ucwords($unePromo->getLibelle()));
                $em = $this->getDoctrine()->getManager();
                $em->persist($unePromo);
                $em->flush();
            }
            return $this->redirect($this->generateUrl('readAllPromotion'));
        }
        return $this->render('CoursPromotionBundle:Promotion:create.html.twig', array('form' => $form->CreateView()));
    }

    /**
     * @Route("/update")
     * @Template()
     */
    public function updateAction($code)
    {
        if (isset($code) && !empty($code))
        {
            $unePromo = new Promotion();

            $rep = $this->getDoctrine()->getManager()->getRepository('CoursQuizzBundle:Promotion');
            $laPromo = $rep->findByCode($code)[0];

            $formBuilder = $this->createFormBuilder()
                    ->add('code', 'text', array('data' => $laPromo->getCode()))
                    ->add('libelle', 'text', array('data' => $laPromo->getLibelle()));

            $form = $formBuilder->getForm();
            $request = $this->get('request');

            if ($request->getMethod() == 'POST')
            {
                $form->bind($request);

                if ($form->isValid())
                {
                    $laPromo->setCode($form['code']->getData());
                    $laPromo->setLibelle(ucwords($form['libelle']->getData()));
                    $em = $this->getDoctrine()->getManager();

                    $em->flush();
                }
                return $this->redirect($this->generateUrl('readAllPromotion'));
            }
            return $this->render('CoursPromotionBundle:Promotion:update.html.twig', array('form' => $form->CreateView(),'code'=>$code));
        }
        else
        {
                return $this->redirect($this->generateUrl('readAllPromotion'));
        }
    }

    /**
     * @Route("/delete")
     * @Template()
     */
    public function deleteAction($code)
    {
        if(isset($code)&&!empty($code)){
            $m = $this->getDoctrine()->getManager()->getRepository('CoursQuizzBundle:Promotion');
            $promo = $m->findByCode($code);
            $u=$this->getDoctrine()->getManager()->getRepository('CoursUserBundle:User')->findByPromotion($promo);
            if(count($u)==0){
                $em = $this->getDoctrine()->getManager();
                $em->remove($promo[0]);
                $em->flush();
            }else{
                $this->setFlash('error', "Des utilisateurs sont présent dans cette promotion, supprimez les puis recommencez.");
            }
        }
        return $this->redirect($this->generateUrl('readAllPromotion'));
    }

    /**
     * @Route("/read")
     * @Template()
     */
    public function readAction()
    {
        $rep = $this->getDoctrine()->getManager();

        $qb = $rep->createQueryBuilder();
        $query=$qb->add('select', 'p')
           ->add('from', 'CoursQuizzBundle:Promotion p')
           ->add('orderBy', 'p.libelle')
            ->getQuery();
        $lesPromos = $query->getResult();

        return $this->render('CoursPromotionBundle:Promotion:read.html.twig', array('promotion' => $lesPromos));
    }

    /**
     * @param string $action
     * @param string $value
     */
    protected function setFlash($action, $value)
    {
        $this->container->get('session')->getFlashBag()->set($action, $value);
    }
}
